<?php
class MessageService
{
	public function getMessageList($rec_module='Message',$rec_id=0,$limit)
	{
		$where = 'rec_id = '.$rec_id.' AND pid = 0 AND reply_type = 0 AND status = 1';
		
		if(!empty($rec_module))
			$where .= " AND rec_module = '$rec_module'";
		
		$limit = (empty($limit)) ? '' : ' LIMIT '.$limit;
		
		$list = array();
		
		$res = DB::query('SELECT * FROM '.DB::table('message').' WHERE '.$where.' 
						ORDER BY is_top DESC,create_time DESC '.$limit);
		
		while($data = DB::fetch($res))
		{
			$data['user_reply'] = array();
			$reply_res = DB::query('SELECT * FROM '.DB::table('message').' 
						WHERE status = 1 AND reply_type = 1 AND pid = '.$data['id'].'
						ORDER BY is_top DESC,create_time DESC '.$limit);
			
			while($reply = DB::fetch($reply_res))
			{
				$reply['create_time_format'] = toDate($reply['create_time']);
				$data['user_reply'][] = $reply;
			}
			
			$admin_reply = array();
			$reply_res = DB::query('SELECT * FROM '.DB::table('message').' 
						WHERE status = 1 AND reply_type = 2 AND pid = '.$data['id'].'
						ORDER BY is_top DESC,create_time DESC '.$limit);
			
			while($reply = DB::fetch($reply_res))
			{
				$reply['create_time_format'] = toDate($reply['create_time']);
				$data['admin_reply'][] = $reply;
			}
			
			$data['create_time_format'] = toDate($data['create_time']);
			
			$list[] = $data;
		}

		return $list;
	}
	
	public function getMessageCount($rec_module='Message',$rec_id=0)
	{
		$where = 'rec_id = '.$rec_id.' AND pid = 0 AND reply_type = 0 AND status = 1';
		if(!empty($rec_module))
			$where .= " AND rec_module = '$rec_module'";
			
		return DB::resultFirst('SELECT COUNT(DISTINCT id) FROM '.DB::table('message').' WHERE '.$where);
	}
}
?>